package com.tiantianfresh.view.service.impl;

import com.tiantianfresh.admin.util.type;
import com.tiantianfresh.model.dao.CommodityMapper;
import com.tiantianfresh.model.dao.OrderMapper;
import com.tiantianfresh.model.dao.OrderSpecificMapper;
import com.tiantianfresh.model.dao.ShoppingCartMapper;
import com.tiantianfresh.model.pojo.CommodityResult;
import com.tiantianfresh.model.pojo.Order;
import com.tiantianfresh.model.pojo.ShoppingCart;
import com.tiantianfresh.view.service.ShoppingCartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;

/**
 * @author FANGAO
 * @date 2021/12/1 - 下午 12:12
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private CommodityMapper commodityMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderSpecificMapper orderSpecificMapper;

    @Override
    public boolean insertShoppingCart(Map<String,Integer> map) {
        CommodityResult commodityResult = commodityMapper.queryCommodity(map.get("comid"));
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setShoppingCartName(commodityResult.getCommodityname());
        shoppingCart.setShoppingCartPath(commodityResult.getCommoditypath());
        shoppingCart.setShoppingCartState(0);
        shoppingCart.setShoppingCartQuantity(map.get("quantityspqn"));
        shoppingCart.setShoppingCartWeight(commodityResult.getCompany());
        if(commodityResult.getStatebackdrop()!=null&&commodityResult.getStatebackdrop().equals("best")){
            double v = type.percentageToDouble(commodityResult.getCommoditystate());

            shoppingCart.setShoppingCartUnitPrice(commodityResult.getPrice()*v);
        }else{
            shoppingCart.setShoppingCartUnitPrice(commodityResult.getPrice()*0.95);
        }
        shoppingCart.setUserid(map.get("userid"));
        //判断是否有同一个商品是的话直接数量+1
        ShoppingCart sc = shoppingCartMapper.selectShoppingCartCount(commodityResult.getCommodityname(), commodityResult.getCompany(), map.get("userid"));
        if (sc!=null) {
            map.put("id",sc.getScid());
            map.put("shoppingCartQuantity",sc.getShoppingCartQuantity()+1);
            shoppingCartMapper.updateQuantity(map);
            return true;
        }else{
            if (shoppingCartMapper.insert(shoppingCart) > 0) return true;
        }
        return false;
    }

    @Override
    public List<ShoppingCart> queryShoppingCartAll(Integer userid) {
        return shoppingCartMapper.queryShoppingCartAll(userid);
    }

    @Override
    public boolean deleteShoppingCart(Integer id) {
        if(shoppingCartMapper.deleteShoppingCart(id)>0) return true;
        return false;
    }

    @Override
    public boolean deleteShoppingCarts(Map<String, Integer[]> map) {
        if(shoppingCartMapper.deleteShoppingCarts(map)>0) return true;
        return false;
    }

    @Override
    public boolean updateQuantityup(Map<String, Integer> map) {
        int i = shoppingCartMapper.selectQuantityid(map.get("id"));
        i++;
        map.put("shoppingCartQuantity",i);
        if(shoppingCartMapper.updateQuantity(map)>0) return true;
        return false;
    }

    @Override
    public boolean updateQuantitydow(Map<String, Integer> map) {
        int i = shoppingCartMapper.selectQuantityid(map.get("id"));
        i--;
        map.put("shoppingCartQuantity",i);
        if(shoppingCartMapper.updateQuantity(map)>0) return true;
        return false;
    }

    @Override
    public boolean updeatState(Map<String, Integer> map) {
        if(shoppingCartMapper.updeatState(map)>0) return true;
        return false;
    }

    @Override
    public Order addOrder(Map<String, Object> map) {
        Order order = new Order();
        order.setOrderid(type.getRandomandDatetime());
        order.setOrderdate(new Date());
        order.setTotalPrice((Double) map.get("countmoney"));
        order.setUserid((Integer) map.get("userid"));
        order.setOrderState(1);
        order.setAddressId(0);
        //if (orderMapper.queryOrderCount(order)>0) {
        //  orderMapper.deleteOrder(order);
        //  orderSpecificMapper.deleteOrderSpecific(order.getOrderid());
        //}

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.queryShoppingCartState((Integer) map.get("userid"));
        Map<String,Integer[]> scids = new HashMap<>();
        List<Integer> scid = new ArrayList<>();
        shoppingCarts.forEach(shoppingCart->scid.add(shoppingCart.getScid()));
        scids.put("scids", scid.toArray(new Integer[scid.size()]));

        if (orderSpecificMapper.addShoppingCarts(shoppingCarts, order.getOrderid()) > 0 && orderMapper.addOrder(order) > 0 && shoppingCartMapper.deleteShoppingCarts(scids)>0) return order;
        return null;
    }
    @Override
    public boolean initializationState(Map<String, Integer> map) {
        if(shoppingCartMapper.initializationState(map)>0) return true;
        return false;
    }
}
